$(function () {
    obj.init();
});

var obj = new function () {
    return {
        init: function () {
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('.btn-syn').click(function () {
                $this.funObj().synCode($(this).attr('data-code'));
            });
        },
        funObj: function () {
            var $this = this;
            return {
                synCode: function (code) {
                    window.top.swal({
                        title: '确认执行同步？',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        allowOutsideClick: true,
                        showCloseButton: true,
                        showLoaderOnConfirm: true,
                        type: 'warning',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                xhExtra.ajaxRequest('config/doSynCode', {code: code}, {success: false, check: true}, function (result) {
                                    window.top.swal({
                                        title: '执行' + (result.var === 0 ? '成功' : '失败'),
                                        html: result.output
                                            ? '<div style="text-align: left; max-height: 600px;">' + result.output + '</div>'
                                            : '<font color="red">无输出</font>'
                                    });
                                });
                            })
                        }
                    }).then(function(isConfirm) {}, function(dismiss) {});
                }
            }
        }
    }
};